package com.atguigu.flink.state;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2022/12/17
 * 
 * 检测每种传感器的水位值，如果连续的两个水位值超过10，就输出报警
 *      使用当前传感器的水位和上一个传感器的水位比对。
 *
 *          保存上一个传感器的水位(单个值)，单值类型的State
 *
 *   -------------------
 *      State.get(): 查询状态的值，读
 *      State.add(): 添加单个值
 *      State.addAll(): 添加多个值
 *      State.clear(): 清空
 *      State.update():  先清空，再写入。类似覆盖写
 *
 *    MapState.put()
 *    MapState.get()
 */
public class Demo3_ValueState
{
    public static void main(String[] args) {
        
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        
                env
                   .socketTextStream("hadoop103", 8888)
                   .map(new WaterSensorMapFunction())
                   .keyBy(WaterSensor::getId)
                   .process(new KeyedProcessFunction<String, WaterSensor, String>()
                   {
                       private ValueState<Integer> lastVc;
                       //在Task初始化时，获取状态。 open()是一个生命周期方法，可以在这个方法中完成状态的获取
                       @Override
                       public void open(Configuration parameters) throws Exception {
                           lastVc = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("lastVc", Integer.class));
                       }

                       @Override
                       public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                            if (value.getVc() > 10 && lastVc.value() != null &&  lastVc.value() > 10 ){
                                out.collect(ctx.getCurrentKey() +"连续两个传感器水位上升...");
                            }

                            //更新状态
                           lastVc.update(value.getVc());

                       }
                   })
                   .print();
        
                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
        
    }
}
